@extends('salesman.layouts.app')

@section('panel_content')

    <div class="aiz-titlebar mt-2 mb-4">
      <div class="row align-items-center">
        <div class="col-md-6">
            <h1 class="h3">{{ translate('Products') }}</h1>
        </div>
      </div>
    </div>

    <div class="card">
        <div class="card-header row gutters-5">
            <div class="col">
                <h5 class="mb-md-0 h6">{{ translate('All Products') }}</h5>
            </div>
            <div class="col-md-4">
                <form class="" id="sort_brands" action="" method="GET">
                    <div class="input-group input-group-sm">
                        <input type="text" class="form-control" id="search" name="search" @isset($search) value="{{ $search }}" @endisset placeholder="{{ translate('Search product') }}">
                    </div>
                </form>
            </div>
        </div>
        <div class="card-body">
            <table class="table aiz-table mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th data-breakpoints="md">
                            {{translate('Thumbnail Image')}}
                        </th>
                        <th width="30%">{{ translate('Name')}}</th>
                        <th data-breakpoints="md">{{ translate('Category')}}</th>
                        <th data-breakpoints="md">{{ translate('Current Qty')}}</th>
                        <th>{{ translate('Base Price')}}</th>
                        @if(get_setting('product_approve_by_admin') == 1)
                            <th data-breakpoints="md">{{ translate('Approval')}}</th>
                        @endif
                        <th data-breakpoints="md">{{ translate('Published')}}</th>
                        <th data-breakpoints="md">{{ translate('Featured')}}</th>
                        <th data-breakpoints="md">{{ translate('Through train promotion')}}</th>
                        <th data-breakpoints="md" class="text-right">{{ translate('Options')}}</th>
                    </tr>
                </thead>

                <tbody>
                    @foreach ($products as $key => $product)
                        <tr>
                            <td>{{ ($key+1) + ($products->currentPage() - 1)*$products->perPage() }}</td>
                            <td>
                                <img
                                    height="80px"
                                    class="lazyload"
                                    src="{{ static_asset('assets/img/placeholder.jpg') }}"
                                    data-src="{{ uploaded_asset($product->thumbnail_img) }}"
                                    alt="{{  $product->getTranslation('name')  }}"
                                    onerror="this.onerror=null;this.src='{{ static_asset('assets/img/placeholder.jpg') }}';"
                                >
                            </td>
                            <td>
                                <a href="{{ route('product', $product->slug) }}" target="_blank" class="text-reset">
                                    {{ $product->getTranslation('name') }}
                                </a>
                            </td>
                            <td>
                                @if ($product->category != null)
                                    {{ $product->category->getTranslation('name') }}
                                @endif
                            </td>
                            <td>
                                @php
                                    $qty = 0;
                                    foreach ($product->stocks as $key => $stock) {
                                        $qty += $stock->qty;
                                    }
                                    echo $qty;
                                @endphp
                            </td>
                            <td>{{ $product->unit_price }}</td>
                            @if(get_setting('product_approve_by_admin') == 1)
                                <td>
                                    @if ($product->approved == 1)
                                        <span class="badge badge-inline badge-success">{{ translate('Approved')}}</span>
                                    @else
                                        <span class="badge badge-inline badge-info">{{ translate('Pending')}}</span>
                                    @endif
                                </td>
                            @endif
                            <td>
                                <label class="aiz-switch aiz-switch-success mb-0">
                                    <input onchange="update_published(this)" value="{{ $product->id }}" type="checkbox" <?php if($product->published == 1) echo "checked";?> >
                                    <span class="slider round"></span>
                                </label>
                            </td>
                            <td>
                                <label class="aiz-switch aiz-switch-success mb-0">
                                    <input onchange="update_featured(this)" value="{{ $product->id }}" type="checkbox" <?php if($product->seller_featured == 1) echo "checked";?> >
                                    <span class="slider round"></span>
                                </label>
                            </td>
                            <td>
                                <label class="aiz-switch aiz-switch-success mb-0">
                                    @if ( count($seller_spread_packages_payments)==0)
                                        @if(isset($product->seller_spread_package_payment->expire_at) && $product->seller_spread_package_payment->expire_at > time())
                                            <input type="checkbox" checked readonly disabled >
                                        @else
                                            <input onchange="toJump()" type="checkbox" >
                                        @endif
                                    @else
                                        @if(isset($product->seller_spread_package_payment->expire_at) && $product->seller_spread_package_payment->expire_at > time())
                                            <input type="checkbox" checked readonly disabled >
                                        @else
                                            <input onchange="select_spread_package({{ $product->id }})" type="checkbox">
                                        @endif
                                    @endif


                                    <span class="slider round"></span>
                                </label>
                            </td>
                            <td class="text-right">
                                <a class="btn btn-soft-warning btn-icon btn-circle btn-sm" style="width: auto"  href="javascript:void(0);" onclick="product_review('{{ $product->id }}','{{ $product->getTranslation('name') }}')" title="{{ translate('Review') }}">
                                    {{ translate('Review') }}
                                </a>
{{--		                      <a class="btn btn-soft-info btn-icon btn-circle btn-sm" href="{{route('seller.products.edit', ['id'=>$product->id, 'lang'=>env('DEFAULT_LANGUAGE')])}}" title="{{ translate('Edit') }}">--}}
{{--		                          <i class="las la-edit"></i>--}}
{{--		                      </a>--}}
{{--                              <a href="{{route('seller.products.duplicate', $product->id)}}" class="btn btn-soft-success btn-icon btn-circle btn-sm"  title="{{ translate('Duplicate') }}">--}}
{{--    							   <i class="las la-copy"></i>--}}
{{--    						  </a>--}}
                              <!--<a href="#" class="btn btn-soft-danger btn-icon btn-circle btn-sm confirm-delete" data-href="{{route('seller.products.destroy', $product->id)}}" title="{{ translate('Delete') }}">-->
                              <!--    <i class="las la-trash"></i>-->
                              <!--</a>-->
                          </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
            <div class="aiz-pagination">
                {{ $products->links() }}
          	</div>
        </div>
    </div>

@endsection

@section('modal')
    @include('modals.delete_modal')
    <!-- Product Review Modal -->
    <div class="modal fade" id="product-review-modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title h6">{{translate('Review')}}</h5>
                    <button type="button" class="close" data-dismiss="modal">
                    </button>
                </div>
                <form action="{{ route('reviews.store') }}"  enctype="multipart/form-data" method="POST">
                    <div id="product-review-modal-content">

                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="opacity-60">{{ translate('Product')}}</label>
                            <p id="product-review-modal-name"></p>
                        </div>
                        <div class="form-group">
                            <label class="opacity-60">{{translate('Customer')}}</label>
                            <div>
                                @php
                                    $customers = \App\Models\User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get();
                                @endphp
                                <select name="user_id" class="form-control aiz-selectpicker pos-customer" data-live-search="true">
                                    @foreach ($customers as $key => $customer)
                                        <option value="{{ $customer->id }}" data-contact="{{ $customer->email }}">
                                            {{ $customer->name }} @if($customer->is_virtual == 1) (<font color="red">{{translate('Virtual')}}</font>) @endif
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="opacity-60">{{ translate('Rating')}}</label>
                            <div class="rating rating-input">
                                <label> <input type="radio" name="rating" value="1" required> <i class="las la-star"></i> </label>
                                <label> <input type="radio" name="rating" value="2"> <i class="las la-star"></i> </label> <label>
                                    <input type="radio" name="rating" value="3"> <i class="las la-star"></i> </label> <label>
                                    <input type="radio" name="rating" value="4"> <i class="las la-star"></i> </label> <label>
                                    <input type="radio" name="rating" value="5"> <i class="las la-star"></i> </label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="opacity-60">{{ translate('Comment')}}</label>
                            <textarea class="form-control" rows="4" name="comment" placeholder="{{ translate('Your review')}}" required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-sm btn-primary">{{translate('Submit review')}}</button>
                        <button type="button" class="btn btn-sm btn-light" data-dismiss="modal">{{translate('Cancel')}}</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- 选择直通车 -->
    <div class="modal fade" id="select_payment_type_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <form class="" id="package_payment_form" action="{{ route('seller.products.spread') }}" method="post">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">{{ translate('Select Spread Package') }}</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" id="product_id" name="product_id" value="">
                        <div class="row">
                            <div class="col-md-2">
                                <label>{{ translate('Spread Package')}}</label>
                            </div>
                            <div class="col-md-10">
                                <div class="mb-3">
                                    <select class="form-control aiz-selectpicker" name="seller_spread_package_payment_id"
                                        data-minimum-results-for-search="Infinity" required>
                                        <option value="">{{ translate('Select One')}}</option>
                                        @foreach ($seller_spread_packages_payments as $key => $seller_spread_packages_payment)
                                            <option value="{{$seller_spread_packages_payment['id']}}">{{ $seller_spread_packages_payment['seller_spread_package']['name'] }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="form-group text-right">
                            <button type="button" class="btn btn-sm btn-primary transition-3d-hover mr-1" id="select_type_cancel" data-dismiss="modal">{{translate('Cancel')}}</button>
                            <button type="submit" class="btn btn-sm btn-primary transition-3d-hover mr-1">{{translate('Confirm')}}</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>



@endsection

@section('script')
    <script type="text/javascript">
        function update_featured(el){
            if(el.checked){
                var status = 1;
            }
            else{
                var status = 0;
            }
            $.post('{{ route('seller.products.featured') }}', {_token:'{{ csrf_token() }}', id:el.value, status:status}, function(data){
                if(data == 1){
                    AIZ.plugins.notify('success', '{{ translate('Featured products updated successfully') }}');
                }
                else{
                    AIZ.plugins.notify('danger', '{{ translate('Something went wrong') }}');
                    location.reload();
                }
            });
        }

        function update_published(el){
            if(el.checked){
                var status = 1;
            }
            else{
                var status = 0;
            }
            $.post('{{ route('salesman.products.published') }}', {_token:'{{ csrf_token() }}', id:el.value, status:status}, function(data){
                if(data == 1){
                    AIZ.plugins.notify('success', '{{ translate('Published products updated successfully') }}');
                }
                else if(data == 2){
                    AIZ.plugins.notify('danger', '{{ translate('Please upgrade your package.') }}');
                    location.reload();
                }
                else{
                    AIZ.plugins.notify('danger', '{{ translate('Something went wrong') }}');
                    location.reload();
                }
            });
        }

        function toJump(){
            AIZ.plugins.notify('danger', '{{ translate('Please open the through train') }}');
            window.location.href = "{{route('seller.seller_spread_packages_list')}}"
        }

        function select_spread_package(product_id){
            $('input[name=product_id]').val(product_id);
            $('#select_payment_type_modal').modal('show');
        }

        function product_review(product_id,productname) {
            $.post('{{ route('product_admin_review_modal') }}', {
                _token: '{{ @csrf_token() }}',
                product_id: product_id
            }, function(data) {
                $('#product-review-modal-content').html(data);
                $('#product-review-modal-name').html(productname);
                $('#product-review-modal').modal('show', {
                    backdrop: 'static'
                });
                AIZ.extra.inputRating();
            });
        }
    </script>
@endsection
